package com.cysyz.mylib.commonservice.statisticsupport.impl;

import javax.annotation.Resource;

import com.cysyz.mylib.commonservice.servicesupport.CommonServiceSupport;
import com.cysyz.mylib.commonservice.statisticsupport.StatisticService;
import com.cysyz.mylib.commonservice.statisticsupport.vo.StatisticableVO;
import com.cysyz.mylib.dao.PagingResult;
import com.cysyz.mylib.queryandcondsannotationparser.QueryAndCondsAnnotationParser;
import com.cysyz.mylib.queryandcondsannotationparser.QueryPrefixPostfixAndConds;
import com.cysyz.mylib.utils.DateUtil;
import com.cysyz.mylib.utils.DateUtil.BeginOrEnd;

public abstract class AbstractStatisticServiceImpl extends CommonServiceSupport implements StatisticService{
	
	@Resource
	private QueryAndCondsAnnotationParser queryAndCondsAnnotationParser;
	
	@Override
	public <T> PagingResult<T> searchAllStatisticList(Class<T> clazz,
			StatisticableVO searchvo, int page, int pageSize) {
	
		dealReportDate(searchvo);
		
		dealVO(searchvo);
		
		return search(clazz, searchvo, page, pageSize);
	}

	protected <T> PagingResult<T> search(Class<T> clazz,
			StatisticableVO searchvo, int page, int pageSize) {
		QueryPrefixPostfixAndConds generateQuery = queryAndCondsAnnotationParser.parse(searchvo);
		return this.listPaging(clazz, generateQuery.getConds(), generateQuery.getQueryPrefix(), generateQuery.getQueryPostfix(), page, pageSize);
	}
	
	protected void dealReportDate(StatisticableVO searchvo){
		String dateType = searchvo.getDateType();
		String dateStart = searchvo.getDateStart();
		String dateEnd = searchvo.getDateEnd();
		if (dateType == null) {
			dateType = "today";
		}
		if ("today".equals(dateType)){
			dateStart = DateUtil.getCurrentDate();	
			dateEnd = DateUtil.getCurrentDate();
		} else if ("yesterday".equals(dateType)) {
			dateStart = DateUtil.getStringDateTime(-1);
			dateEnd = DateUtil.getStringDateTime(-1);
		} else if ("7days".equals(dateType)){
			dateStart = DateUtil.getStringDateTime(-7);
			dateEnd =DateUtil.getCurrentDate();
		} 
		
		if (dateStart != null && dateStart.isEmpty()) {
			dateStart =DateUtil.getCurrentDate();
		}
		if (dateEnd != null && dateEnd.isEmpty()) {
			dateEnd = DateUtil.getCurrentDate();
		}
		
		searchvo.setDateStart(dateStart);
		searchvo.setDateEnd(dateEnd);
		searchvo.setStartDateDB(DateUtil.getIntDateTime(dateStart));
		searchvo.setEndDateDB(DateUtil.getIntDateTime(dateEnd, BeginOrEnd.end));
	}

	protected void dealVO(StatisticableVO searchvo) {};
	
	public static void main(String[] args) {
		System.out.println(DateUtil.getDateTime());
		//String currentDate = DateUtil.getIntDateTime("2014-02-19");
		System.out.println(DateUtil.getIntDateTime("2014-05-07"));
		System.out.println(DateUtil.getIntDateTime("2014-05-07", BeginOrEnd.end));
	}

	public QueryAndCondsAnnotationParser getQueryAndCondsAnnotationParser() {
		return queryAndCondsAnnotationParser;
	}

	public void setQueryAndCondsAnnotationParser(
			QueryAndCondsAnnotationParser queryAndCondsAnnotationParser) {
		this.queryAndCondsAnnotationParser = queryAndCondsAnnotationParser;
	}
}
